package com.blue.base.common.utils.statistic;

import com.blue.base.common.utils.date.LocalDateUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liulei
 * @version 1.0
 */
public class DataStatisticUtil {

    private static final String DATE_KEY = "dayTime";
    private static final String DATE_COUNT_KEY = "count";

    private DataStatisticUtil() {
        throw new IllegalArgumentException("无法实例化");
    }

    /**
     * 获取最近X天的默认统计数据[不包含当天]
     *
     * @param days         最近X天
     * @param includeToday 是否包含当天
     * @return 默认统计数据
     */
    public static List<Map<String, Object>> latestDayDefaultStatistic(int days, boolean includeToday) {
        if (days < 0) {
            throw new IllegalArgumentException("非法入参");
        }
        List<Map<String, Object>> week = Lists.newArrayListWithCapacity(days);
        for (int i = (days - 1); i >= 0; i--) {
            Map<String, Object> dayMap = new HashMap<>(2);
            int index = includeToday ? -i : -i - 1;
            dayMap.put(DATE_KEY, LocalDateUtils.getDateAfterBySimpleDateFormat(index, LocalDateUtils.DATE_FORMAT));
            dayMap.put(DATE_COUNT_KEY, 0);
            week.add(dayMap);
        }
        return week;
    }

    /**
     * 获取当年的默认统计数据
     *
     * @return 默认统计数据
     */
    public static List<Map<String, Object>> latestYearDefaultStatistic() {
        List<Map<String, Object>> month = Lists.newArrayListWithCapacity(12);
        int year = LocalDate.now().getYear();

        for (int i = 1; i <= 12; i++) {
            Map<String, Object> monthMap = new HashMap<>();
            monthMap.put(DATE_KEY, year + String.format("-%02d", i));
            monthMap.put(DATE_COUNT_KEY, 0);
            month.add(monthMap);
        }
        return month;
    }

    /**
     * 整理日期统计数据[自动补全缺失日期，形成带默认值的日期统计map集合[{"date}:"2021-04-02","count":2]]
     *
     * @param startDateStr 开始日期
     * @param endDateStr   结束日期
     * @param dbResult     数据库查询结果
     * @return 统计结果
     * @throws ParseException 转换异常
     */
    public static List<Map<String, Object>> fillDbDateCountList(String startDateStr, String endDateStr, List<Map<String, Object>> dbResult) throws ParseException {
        List<Map<String, Object>> result = new ArrayList<>();
        List<LocalDate> list = LocalDateUtils.getAscDateList(startDateStr, endDateStr);
        list.forEach(date -> {
            String dateStr = LocalDateUtils.formatLocalDate(date, LocalDateUtils.DATE_FORMAT);
            Map<String, Object> map = Maps.newHashMapWithExpectedSize(2);
            // 必须与数据结果集合中字段保持一致
            map.put(DATE_KEY, dateStr);
            map.put(DATE_COUNT_KEY, 0);
            result.add(map);
        });
        // 适配数据库db记录
        for (Map<String, Object> map : result) {
            String dayTime = (String) map.get(DATE_KEY);
            for (Map<String, Object> dbMap : dbResult) {
                String format = new SimpleDateFormat(LocalDateUtils.DATE_FORMAT).format(dbMap.get(DATE_KEY));
                if (dayTime.equals(format)) {
                    map.put(DATE_COUNT_KEY, dbMap.get(DATE_COUNT_KEY));
                    break;
                }
            }
        }
        return result;
    }

}
